import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.JLabel;
import javax.swing.SpinnerNumberModel;
import javax.swing.JPanel;
import javax.swing.JColorChooser;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.LineBorder;

/**
 * GVertexDialog is the dialog for modifying various properties of a vertex.
 *
 * @version 1.0
 * @author P.Jaya Krishna.
 */
class GVertexDialog extends JDialog implements ActionListener
{
	JButton changeFillColor;
	JButton changeColor;
	JButton ok;
	JButton cancel;
	JTextField name;
	JSpinner height;
	JSpinner width;
	JLabel colorlabel;
	JLabel fillcolorlabel;
	Color vertexFillColor;
	Color vertexColor;
	
	GPanel gPanel;
	VertexAttr currentVertex;

	
	/**
	 * Creates a dialog for the specified vertex "currVertex" of a GPanel "gPanel".
	 */
	public GVertexDialog(GPanel gPanel, VertexAttr currVertex)
	{
		super(gPanel.gFrame, "Vertex Properties", true);
		this.gPanel = gPanel;
		currentVertex = currVertex;
		vertexColor = currentVertex.getColor();
		vertexFillColor = currentVertex.getFillColor();
		
		changeFillColor = new JButton("Change");
		changeColor     = new JButton("Change");
		ok              = new JButton("Ok");
		cancel			= new JButton("Cancel");
		width           = new JSpinner(new SpinnerNumberModel(currentVertex.getWidth(), 1,100,2));
		height          = new JSpinner(new SpinnerNumberModel(currentVertex.getHeight(), 1, 100,2));
		name            = new JTextField(currentVertex.getName());
		
		JPanel buttonpanel = new JPanel();
		buttonpanel.add(ok);
		buttonpanel.add(cancel);
		JPanel prop = new JPanel();
		prop.setLayout(null);
		
		JLabel lname = new JLabel("Name");
		lname.setFont(new Font("Garmond", Font.BOLD, 13));
		lname.setBounds(10, 10, 100,20);  //x, y, width, height
		JLabel lheight = new JLabel("Height");
		lheight.setBounds(10, 35, 100,20);
		lheight.setFont(new Font("Garmond", Font.BOLD, 13));
		JLabel lwidth = new JLabel("Width");
		lwidth.setBounds(10, 60, 100,20);
		lwidth.setFont(new Font("Garmond", Font.BOLD, 13));
		JLabel lcolor = new JLabel("Border Color");
		lcolor.setBounds(10, 85, 100,20);
		lcolor.setFont(new Font("Garmond", Font.BOLD, 13));
		JLabel lfcolor = new JLabel("Fill Color");
		lfcolor.setBounds(10, 110, 100,20);
		lfcolor.setFont(new Font("Garmond", Font.BOLD, 13));
		
		name.setBounds(110, 10, 105,20);
		name.setHorizontalAlignment(JTextField.RIGHT);
		height.setBounds(110, 35, 105,20);
		width.setBounds(110, 60, 105,20);
		changeColor.setBounds(135, 85, 80,20);
		changeFillColor.setBounds(135, 110, 80,20);
		
		colorlabel = new JLabel("  ");
		colorlabel.setBorder(new LineBorder(vertexColor, 8) );
		colorlabel.setBounds(110, 88, 16,16);
		prop.add(colorlabel);
		fillcolorlabel = new JLabel("  ");
		fillcolorlabel.setBorder(new LineBorder(vertexFillColor, 8) );
		fillcolorlabel.setBounds(110, 113, 16,16);
		prop.add(fillcolorlabel);
		
		prop.add(lname);
		prop.add(name);
		prop.add(lheight);
		prop.add(height);
		prop.add(lheight);
		prop.add(width);
		prop.add(lwidth);
		prop.add(changeColor);
		prop.add(lcolor);
		prop.add(lfcolor);
		prop.add(changeFillColor);
		
		getContentPane().add(prop, BorderLayout.CENTER);
		getContentPane().add(buttonpanel, BorderLayout.SOUTH);
		
		changeFillColor.addActionListener(this);
		changeColor.addActionListener(this);
		ok.addActionListener(this);
		cancel.addActionListener(this);
		
		setSize(250,200);
		setResizable(false);
		setLocation(500,100);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource().equals(changeFillColor))
		{
			JColorChooser cc = new JColorChooser();
			Color c = cc.showDialog(gPanel.gFrame, "Color Chooser", currentVertex.getFillColor());
			if(c!=null)
			{
				fillcolorlabel.setBorder(new LineBorder(c, 8));
				vertexFillColor = c;
			}
		}
		else if(ae.getSource().equals(changeColor))
		{
			JColorChooser cc = new JColorChooser();
			Color c = cc.showDialog(gPanel.gFrame, "Color Chooser", currentVertex.getColor());
			if(c!=null)
			{
				colorlabel.setBorder(new LineBorder(c, 8));
				vertexColor = c;
			}	
		}
		else if(ae.getSource().equals(ok))
		{
			dispose();
			Number h = (Number) height.getValue();
			currentVertex.setHeight(h.intValue());
			Number w = (Number) width.getValue();
			currentVertex.setWidth(w.intValue());
			currentVertex.setName(name.getText());
			currentVertex.setColor(vertexColor);
			currentVertex.setFillColor(vertexFillColor);
		}
		else if(ae.getSource().equals(cancel))
		{
			dispose();
		}
	}
}